from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response

from loc_data import TRANSLATIONS

from models import Product,  DEFAULT_LANG,  TranslationsXmlContent, TEST_TOP_PRODUCTS, flush_model
from xml.dom.minidom import parse, parseString
import logging, os, sys

#translations
TRANSLATION_PREFIX = "loc_"
TRANSLATION_GROUP_MULTIPLIER = 1000
LANG_SESSION_KEY_NAME = "lang"
def current_lang(lang):
	session = sessions.Session()
	#retrive from session if no lang set
	if not lang:
		if LANG_SESSION_KEY_NAME in session: 
			lang = session[LANG_SESSION_KEY_NAME]
	#check if lang is valid
	if  not TRANSLATIONS.has_key(lang):
		lang=DEFAULT_LANG
	#save to session and return
	session[LANG_SESSION_KEY_NAME] = lang
	return lang


def add_translation_labels(labels_dict, lang):
	xml_content = TranslationsXmlContent.get_or_insert('name:'+lang, name=lang, content=TRANSLATIONS[lang])
	xml_translations = parseString(xml_content.content.encode('utf-8'))
	for translationGroup in xml_translations.getElementsByTagName('translationGroup'):
		groupId = int(translationGroup.getAttribute('groupId'))
		groupMultiplier = groupId * TRANSLATION_GROUP_MULTIPLIER
		for translation in translationGroup.getElementsByTagName('translation'):
			labelId = int(translation.getAttribute('labelId'))
			label_key = TRANSLATION_PREFIX + str(groupMultiplier+labelId)
			labels_dict[label_key] = translation.firstChild.wholeText
	

from google.appengine.ext.db import delete
def update_loc(request):
	flush_model(TranslationsXmlContent)
	return HttpResponseRedirect("/")

def localize(list, translation_labels):
	return [item.set_translation_labels(translation_labels) for item in list]


from google.appengine.api import urlfetch
from appengine_utilities import sessions
def default(request, lang=''):
	lang = current_lang(lang)
	template_data = {}
	add_translation_labels(template_data, DEFAULT_LANG) #ensure all keys set first
	add_translation_labels(template_data, lang)
	flush_model(Product)
	template_data['top_products'] = localize(TEST_TOP_PRODUCTS, template_data)
	template_data['lang'] = lang
	return render_to_response('splash.htm', template_data)


RECAPTCHA_PUB_KEY = "6Lc0-wIAAAAAAPgO_wwS9asHasMchdpDh6TLRBde"
RECAPTCHA_PRIVATE_KEY = "6Lc0-wIAAAAAAAxwnmitbKPRWYORH_JLsqvHBP36"


from django import newforms as forms

class ContactForm(forms.Form):
    message = forms.CharField()

from os import environ
from recaptcha.client import captcha
def guestbook(request):
	if request.method == 'POST':
		return HttpResponseRedirect('/url/on_success/')
	else:
		chtml = captcha.displayhtml(
								 public_key = RECAPTCHA_PUB_KEY,
	  						     use_ssl = False,
	  						     error = None)
		form = ContactForm()
		template_data = {
						 'form':form,
						 'captchahtml':chtml,
						 }
	return render_to_response('captcha.htm', template_data)


#def test_captcha(request):


#	template_data = {
	   
#	}
#	return render_to_response('captcha.htm', template_data)

	

